import {
  ref
} from 'vue';
import { formatDate, FormatTemplate } from 'pub-bbx-utils';
/** 
 * @description 获取下载图表的id/name
*/
export function useChartDownloadData(chartType, type) {
  let chartDomId = ''
  let chartDownloadName = '下载'
  switch (chartType) {
    case 'average-response-time':
      chartDownloadName = '平均响应用时'
      chartDomId = 'BarChart'
      break
    case 'average-service-time':
      chartDownloadName = '平均服务用时'
      chartDomId = 'BarChart'
      break
    case 'average-maintenance-time':
      chartDownloadName = '平均维修用时'
      chartDomId = 'BarChart'
      break
    case 'timely-closing-rate':
      chartDownloadName = '及时关单率'
      chartDomId = 'BarLineChart'
      break
    case 'service-human-efficiency':
      chartDownloadName = '服务人效'
      chartDomId = 'BarChart'
      break
    case 'repeated-maintenance-rate':
      chartDownloadName = '重复维修率'
      chartDomId = 'BarLineChart'
      break
    case 'task-return-visit':
      chartDownloadName = '工单回访率'
      chartDomId = 'StackedLineChart'
      break
    case 'task-customer-satisfaction':
      if (type === 'PIE') {
        chartDownloadName = '工单客户满意度-满意度构成'
        chartDomId = 'PieChart'
      } else if (type === 'LINE') {
        chartDownloadName = '工单客户满意度-好评率'
        chartDomId = 'StackedLineChart'
      }
      break
    case 'spare-parts-turnover':
      chartDownloadName = '库存周转率'
      chartDomId = 'BarLineChart'
      break
    case 'spare-parts-satisfy':
      chartDownloadName = '备件满足率'
      chartDomId = 'BarChart'
      break
    case 'revenue-expenditure-analysis':
      if (type === 'INCOMEPIE') {
        chartDownloadName = '收支结构分析-总收入'
        chartDomId = 'IncomePiechart'
      } else if (type === 'PAYPIE') {
        chartDownloadName = '收支结构分析-总支出'
        chartDomId = 'PayPieChart'
      } else if (type === 'PIEPROFIT') {
        chartDownloadName = '收支结构分析-总利润'
        chartDomId = 'PieProfitChart'
      } else if (type === 'TEAMINCOME') {
        chartDownloadName = '收支结构分析-团队排行榜-部门收入排行'
        chartDomId = 'TeamIncome'
      } else if (type === 'TEAMPROFIT') {
        chartDownloadName = '收支结构分析-团队排行榜-部门利润排行'
        chartDomId = 'TeamProfit'
      } else if (type === 'STAFFINCOME') {
        chartDownloadName = '收支结构分析-团队排行榜-人员收入排行'
        chartDomId = 'staffIncome'
      } else if (type === 'STAFFPROFIT') {
        chartDownloadName = '收支结构分析-团队排行榜-人员利润排行'
        chartDomId = 'staffProfit'
      }
      break
    case 'spare-part-sale':
      if (type === 'SPAREINCOME') {
        chartDownloadName = '备件营收分析-备件明细';
        chartDomId = 'spareIncomeProfit';
      } else if (type === 'SERVICEINCOME') {
        chartDownloadName = '备件营收分析-服务项目明细';
        chartDomId = 'serviceIncomeProfit';
      } else if (type === 'COUNTSUMMARY') {
        chartDownloadName = '备件营收分析-备件数量统计';
        chartDomId = 'countSummary';
      } else if (type === 'AMOUNTSUMMARY') {
        chartDownloadName = '备件营收分析-备件金额统计';
        chartDomId = 'amountSummary';
      }
      break;
    default:
      break
    }
  return {
    chartDomId,
    chartDownloadName
  }
}

export function useBarChartData(chartData) {
  const data = ref([]);
  const xAxisData = ref([]);

  if (chartData.length !== 0) {
    // let multiple = 1;

    // if (chartOption === 'h') {
    //   multiple = 60;
    // } else if (chartOption === 'd') {
    //   multiple = 60 * 24;
    // }

    chartData.forEach(item => {
      const num = item.avgTime || item.taskCount || item.countValue || 0;
      data.value.push(num);
      xAxisData.value.push(formatDate(item.statisticsTime, FormatTemplate.date) || item.timeNode);
    });
  }

  return {
    data,
    xAxisData
  }
}

export function useBarChartOption() {
  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: [{
      type: 'category',
      axisTick: {
        alignWithLabel: true,
      },
    }, ],
    yAxis: [{
      nameTextStyle: {
        align: 'right',
        verticalAlign: 'bottom',
        padding: [9, 0, 15, 9],
      },
      type: 'value',
      axisLine: {
        show: false,
      },
      splitLine: {
        // y轴线颜色
        lineStyle: {
          color: ['#e2e7eb'],
        },
      },
      axisTick: {
        show: false,
      },
    }, ],
    series: [{
      type: 'bar',
      barWidth: '50%',
    }, ],
  }
}

export function useLineChartLegend(obj = {}) {
  const legendData = ref([]);
  if (!obj.aroundSearchQueryList) return []
  if (Object.keys(obj).length !== 0 && obj.aroundSearchQueryList.length !== 0) {
    let unit = '天';
    if (obj.closeDimension === 0) {
      unit = '分钟';
    } else if (obj.closeDimension === 1) {
      unit = '小时';
    }

    legendData.value = obj.aroundSearchQueryList.map(item => {
      return `${item.startIndex}-${item.endIndex}${unit}`;
    })
  }

  return legendData;
}

export function useLineChartData(lineChartData) {
  const lineData = ref([]);
  const legendData = ref([])
  if (lineChartData.length !== 0) {
    lineChartData.forEach((item, a) => {
      // const arr = item.dataDeliveryFormDTOS.map((subItem, b) => subItem.taskCount + a + b);
      const arr = item.dataDeliveryFormDTOS.map((subItem, b) => subItem.percentCount)
      lineData.value.push(arr);
      legendData.value.push(item.statisticsAroud)
    })
  }

  return {
    lineData,
    legendData
  };
}

export function useStackedLineChartData(chartData) {
  const data = ref([])
  const legendData = ref([])
  const xAxisData = ref([])

  if (chartData.length > 0) {

    // 折线数据
    data.value = chartData.map((item, index) => {
      
      legendData.value.push(item.name)
      // 工单回访率reviewFormDTOList 工单客户满意度dataSatisfactionBrokenDTOList
      let reviewFormDTOList = item.reviewFormDTOList || item.dataSatisfactionBrokenDTOList || []
      if( index === 0 ) {
        // x轴数据
        reviewFormDTOList.forEach(v => {
          xAxisData.value.push(formatDate(v.statisticsTime, FormatTemplate.date))
        })
      }
      let seriesData = reviewFormDTOList.map(v => {
        return v.taskCount || v.avgCount || 0
      })

      return {
        name: item.name,
        id: item.id,
        type: 'line',
        data: seriesData
      }
    })
  }

  return {
    data,
    legendData,
    xAxisData
  }
}

export function useBarRankingChartData(chartData, typeValue = false) {
  let data = ref([])
  let yAxisData = ref([])
  data.value = chartData.map(item => {
    yAxisData.value.push(item?.name)

    let data = {
      value: item?.countNums,
      staffId: item?.id,
      profitRate: item.profitRate ? `${item.profitRate}%` : '0%'
    };
    if(typeValue) data = item.value;
    
    return data;
  })
  return {
    data,
    yAxisData
  }
}

/** 
 * @description 处理折柱图数据
*/
export function useBarLineChartData(props) {
  const legendList = ref([])
  const xAxisList = ref([])
  const barList = ref([])
  const lineList = ref([])
  const barTitle = ref('')
  const lineTitle = ref('')
  const lineSeries = ref([])

  // 及时关单率
  if (props.chartType === 'timely-closing-rate') {
    const { data, xAxisData } = useBarChartData(props.chartData)

    // const legendData = useLineChartLegend(props.chartOption)

    const { lineData , legendData } = useLineChartData(props.lineChartData)

    legendList.value = legendData.value
    xAxisList.value = xAxisData.value
    barList.value = data.value
    lineList.value = lineData.value
    barTitle.value = '工单数量'

    lineSeries.value = lineList.value.map((item, index) => {
      return {
        name: legendList.value[index],
        type: 'line',
        smooth: true,
        yAxisIndex: 1,
        data: item,
      };
    });
  }

  if (props.chartType === 'spare-parts-turnover') {
    // 库存周转率
    const { barDayData, lineRateData, xData } = useTurnoverLineChartData(props.chartData)
    xAxisList.value = xData.value // x轴数据
    barList.value = barDayData.value // 柱状图数据
    lineList.value = lineRateData.value // 折线数据
    barTitle.value = '备件周转天数'
    lineTitle.value = '备件周转率'
    lineSeries.value = [
      {
        name: lineTitle.value,
        type: 'line',
        smooth: true,
        yAxisIndex: 1,
        data: lineList.value,
      }
    ]
  }

  if (props.chartType === 'repeated-maintenance-rate') {
    // 重复维修率
    const { barDayData = [], lineRateData = [], xData = [], legendData = [] } = useRepeatedMaintenanceRateChartData(props)
    xAxisList.value = xData.value // x轴数据
    barList.value = barDayData.value // 柱状图数据
    lineSeries.value = lineRateData.value // 折线数据
    legendList.value = legendData.value
  }
  return {
    legendList,
    xAxisList,
    barList,
    lineList,
    barTitle,
    lineTitle,
    lineSeries
  }
}

/** 
 * @description 库存周转率图表数据
*/
export function useTurnoverLineChartData (chartData) {
  const barDayData = ref([])
  const lineRateData = ref([])
  const xData = ref([])
  barDayData.value = chartData.map((item, index) => {
    lineRateData.value.push(item.turnoverRate)
    xData.value.push(item.time)
    return item.turnoverDays
  })
  return {
    barDayData,
    lineRateData,
    xData
  }
}

/** 
 * @description 重复维修率图表数据
*/
export function useRepeatedMaintenanceRateChartData(props) {
  const barDayData = ref([])
  const lineRateData = ref([])
  const xData = ref([])
  const legendData = ref([])
  barDayData.value = props.chartData?.map((barItem, barIndex) => {
    xData.value.push(formatDate(barItem.statisticsTime, FormatTemplate.date))
    return barItem.taskCount || 0;
  })
  lineRateData.value = props.lineChartData?.map((lineItem, lineIndex) => {
    legendData.value.push(lineItem.statisticsAroud)
    let dataDeliveryFormDTOS = lineItem.dataDeliveryFormDTOS || []
    let arr =  dataDeliveryFormDTOS.map((item, index) => {
      return item.percentCount
    })
    return {
      name: lineItem.statisticsAroud,
      type: 'line',
      smooth: true,
      yAxisIndex: 1,
      data: arr
    }
  })
  return {
    barDayData,
    lineRateData,
    xData,
    legendData
  }
}

/** 
 * @description 处理下钻table数据
*/
const columnsType = {
  MAN: '人员',
  PRODUCT: '产品',
  CUSTOMER: '客户',
  SERVICEPROVIDER: '团队'
}
export function builsTabsData(props) {
  let commonTabs = [
    {
      label: 'MAN',
      value: '人员'
    },
    {
      label: 'PRODUCT',
      value: '产品'
    }
  ]
  if (props.chartType === 'repeated-maintenance-rate') {
    return [
     ...commonTabs
    ]
  } else if (props.chartType === 'task-customer-satisfaction') {
    return [
      ...commonTabs,
      {
        label: 'CUSTOMER',
        value: '客户'
      },
      {
        label: 'SERVICEPROVIDER',
        value: '团队'
      }
    ]
  } else {
    return []
  }
}

/** 
 * @description 及时关单率/重复维修率 周期tabs数据
*/
export function buildsPeriodTabsData(props) {
  const periodTabsData = ref([])
  if (props.chartType === 'timely-closing-rate') {
    const unit = {
      0: '分钟',
      1: '小时',
      2: '天'
    }
    const aroundSearchQueryList = props.chartOption.aroundSearchQueryList || []
    const closeDimension = unit[props.chartOption.closeDimension]
    periodTabsData.value = aroundSearchQueryList.map((item, index) => {
      return {
        label: index,
        value: `${item.startIndex}-${item.endIndex}${closeDimension}`
      }
    })
  }

  if (props.chartType === 'repeated-maintenance-rate') {
    const repeatedSearchQueries = props.chartOption.repeatedSearchQueries || []
    periodTabsData.value = repeatedSearchQueries.map((item, index) => {
      return {
        label: index,
        value: `${item.searchIndex}天`
      }
    })
  }
  
  return periodTabsData.value
}

export function buildColumns(props) {

  const chartType = type => type === props.chartType

  if (['average-response-time','average-service-time','average-maintenance-time'].findIndex(chartType) > -1) {
    // 平均响应用时 平均服务用时 平均维修用时
    return {
      columnsData: buildTimeColumns(props),
      tableData: buildTimeTableData(props)
    }
  } else if (['timely-closing-rate'].findIndex(chartType) > -1) {
    // 及时关单率
    return {
      columnsData: buildCloseColumns(props),
      closingData: buildCloseTableData(props)
    }
  } else if (['service-human-efficiency'].findIndex(chartType) > -1) {
    // 服务人效
    const tableData = props.tableData.map(item => {
      item.taskCount = `${item.taskCount}单/天`
      item.userStaffId = item.staffId || '';
      return item
    })
    return {
      columnsData: buildServiceColumns(props),
      // TODO: 微信人员open-data
      tableData
    }
  } else if(['repeated-maintenance-rate'].findIndex(chartType) > -1) {
    // 重复维修率
    let { columnsData, tableData, closingData } = buildRepeatedMaintenanceRateTable(props)
    return {
      columnsData,
      tableData,
      closingData
    }
  } else if (['task-return-visit'].findIndex(chartType) > -1) {
    // 工单回访
    return {
      columnsData: buildTaskColumns(props),
      tableData: buildTaskTableData(props)
    }
  } else if (['task-customer-satisfaction'].findIndex(chartType) > -1) {
    // 工单客户满意度
    if (props.lineTableData.length > 0) {
      // 折线下钻数据
      let { columnsData, tableData} = buildTaskSatisfiedDegreePraiseTable(props)
      return {
        columnsData,
        tableData
      }
    }
    return {
      columnsData: buildTaskSatisfiedDegreeColumns(props),
      tableData: buildTaskSatisfiedDegreeTableData(props)
    }
  }
}

function buildTimeColumns(props) {
  try {
    // 单位
    let unit = {
      1: 'm',
      2: 'h',
      3: 'd'
    }

    let firstColumns = {
      label: `人员/单位${unit[props.timeUnit]}`,
      prop: 'userName',
      tooltip: true,
      minWidth: '150px'
    }

    // 构造table header
    let formData = props.tableData[0]?.formData || []
    let columns = ref([])
    columns.value = formData.map((item, index) => {
      return {
        label: item.statisticsTime,
        prop: `${item.statisticsTime}_${index}`,
        tooltip: true,
        minWidth: '100px'
      }
    })
    columns.value?.unshift(firstColumns)
    return columns.value || []
  } catch(e) {
    console.log(e)
  }
}

function buildTimeTableData(props) {
  try {
    let arr = ref([])
    props.tableData.forEach((item,index) => {
      
      let formData = item.formData || []
      let obj = {}
      

      formData.forEach((time, timeIndex) => {
        let keys = `${time.statisticsTime}_${timeIndex}`
        obj[keys] = time.avgTime || '-'
      })
      
      arr.value.push(obj)
      arr.value[index].id = item.userId;
      arr.value[index].userName = item.userName
      // TODO: 微信人员open-data
      arr.value[index].userStaffId = item.staffId || ''
    })

    return arr.value || []
  } catch(e) {
    console.log(e)
  }
}

function buildCloseColumns(props) {
  try {
    const type = props?.chartOption?.selectType;
    let firstColumns = {
      label: `${type === 'PRODUCT' ? '产品类型' : '人员'}/单位: %`,
      prop: 'userName',
      tooltip: true,
      minWidth: '150px'
    }
  
    // 构造table header
    let columns = ref([])
    let dataTurnAroundFormDTOS = props.tableData[0]?.dataDeliveryFormDTOS[0]?.dataTurnAroundFormDTOS || []
    columns.value = dataTurnAroundFormDTOS.map((item,index) => {
      return {
        label: item.statisticsTime,
        prop: `${item.statisticsTime}_${index}`,
        tooltip: true,
        minWidth: ''
      }
    })
    columns.value?.unshift(firstColumns)

    return columns.value || []
  } catch(e) {
    console.log(e)
  }
}

function buildCloseTableData(props, type = '-') {
  try {
    const selectType = props?.chartOption?.selectType;
    let arr = ref([])
    props.tableData.forEach((item,index) => {

      let obj = {}
      obj['statisticsAroud'] = item.statisticsAroud
      obj['tableList'] = []

      let dataDeliveryFormDTOS = item.dataDeliveryFormDTOS || []

      dataDeliveryFormDTOS.forEach((item2,index2) => {

        let obj2 = {}
        obj2['userName'] = item2.name
        obj2['id'] = item2.id;
        obj2['selectType'] = selectType;
        // TODO: 微信人员open-data
        obj2['userStaffId'] = item2.staffId || ''

        let dataTurnAroundFormDTOS = item2.dataTurnAroundFormDTOS || []

        dataTurnAroundFormDTOS.forEach((item3,index3) => {

          let keys = `${item3.statisticsTime}_${index3}`
          let isMostKey = `${keys}_most`
          let taskIdKeys = `${keys}_taskId`
          obj2[keys] = item3.percentCount ? `${item3.percentCount}%` : type
          obj2[isMostKey] = item3.isMost
          obj2[taskIdKeys] = item3.ids
        })

        obj['tableList'].push(obj2)
      })

      arr.value.push(obj)
      
    })

    return arr.value || []

  } catch(e) {
    console.log(e)
  }
}

function buildServiceColumns(props) {
  try {
    let firstColumns = ref([])
    firstColumns.value = [
      {
        label: '人员',
        prop: 'userName',
        tooltip: true,
        minWidth: '150px'
      },
      {
        label: '总工单量',
        prop: 'totalCount',
        tooltip: true,
        minWidth: ''
      },
      {
        label: '工时标准(天)',
        prop: 'workTimeDay',
        tooltip: true,
        minWidth: ''
      },
      {
        label: '工作效率',
        prop: 'taskCount',
        tooltip: true,
        minWidth: ''
      },
    ]
    return firstColumns.value || []
  } catch(e) {
    console.log(e)
  }
}

function buildTaskColumns(props, chartType) {
  try {
    let firstColumns = ref([])
    firstColumns.value = [
      {
        label: '工单编号',
        prop: 'taskNo',
        tooltip: true,
        minWidth: 150
      },
      {
        label: '工单类型',
        prop: 'templateName',
        tooltip: true,
        minWidth: 100
      },
      {
        label: '客户',
        prop: 'customerName',
        minWidth: 80
      },
      {
        label: '联系人',
        prop: 'lmName',
        minWidth: 150
      },
      {
        label: '电话',
        prop: 'customerPhone',
        minWidth: 150
      },
      {
        label: '产品',
        prop: 'productName',
        minWidth: 150
      },
      {
        label: '服务类型',
        prop: 'serviceType',
        minWidth: 150
      },
      {
        label: '负责人',
        prop: 'userName',
        minWidth: 100
      },
      {
        label: '协同人',
        prop: 'synergiesName',
        minWidth: 100
      },
      {
        label: '回访人',
        prop: 'reviewUserName',
        minWidth: 100
      },
      {
        label: 'CSAT满意度',
        prop: 'degree',
        minWidth: 100
      },
      {
        label: '回访时间',
        prop: 'evaluateTime',
        minWidth: 100
      },
      {
        label: '回访备注',
        prop: 'evaluateContent',
        minWidth: 100
      },
      {
        label: '完成时间',
        prop: 'complateTime',
        minWidth: 150
      }
    ]
    if (chartType === 'task-customer-satisfaction') {
      firstColumns.value = [
        {
          label: '工单编号',
          prop: 'taskNo',
          tooltip: true,
          minWidth: 150
        },
        {
          label: '工单类型',
          prop: 'templateName',
          tooltip: true,
          minWidth: 100
        },
        {
          label: '客户',
          prop: 'customerName'
        },
        {
          label: '负责人',
          prop: 'userName',
          minWidth: 100
        },
        {
          label: '评价时间',
          prop: 'evaluateTime',
          minWidth: 150
        },
        {
          label: 'CSAT满意度',
          prop: 'degree',
          minWidth: 100
        }
      ]
    }

    // 去掉服务效率相关字段
    // let starsColumns = ref([])
    // let stars = props.tableData[0]?.stars || []
    // stars.forEach((item, index) => {
    //   starsColumns.value.push({
    //     label: item.title,
    //     prop: `starEvaluate${index + 1}`,
    //     minWidth: 100
    //   })
    // })
    // return [...firstColumns.value, ...starsColumns.value]
    return firstColumns.value
  } catch (e) {
    console.log(e)
  }
}

function buildTaskTableData(props) {
  let arr = ref([])
  arr.value = props.tableData.map((item, index) => {
    // 后端不转时间格式
    // let evaluateTime = safeNewDate(item.evaluateTime)
    // item.evaluateTime = `${evaluateTime.getFullYear()}-${evaluateTime.getMonth() + 1<10?`0${evaluateTime.getMonth() + 1}`:evaluateTime.getMonth() + 1}-${evaluateTime.getDate()}`
    
    // 暂时去掉服务效率相关
    // let obj = {}
    // item.stars.forEach((stars,starsIndex) => {
    //   let keys = `starEvaluate_${starsIndex}`
    //   obj[keys] = stars.value
    // })
    // TODO: 微信人员open-data
    item['userStaffId'] = item.staffId || '';
    
    // let filterItem = Object.assign(item, obj)
    return item
  })
  return arr.value || [] 
}

function buildTaskSatisfiedDegreeColumns(props) {
  const columns = [
    {
      label: columnsType[props.chartOption?.ousideSelectType],
      prop: 'name',
      tooltip: true,
      minWidth: '150px'
    },
    {
      label: '满意度：非常满意',
      prop: 'satisfied_0',
      tooltip: true,
      minWidth: '150px'
    },
    {
      label: '满意度：满意',
      prop: 'satisfied_1',
      tooltip: true,
      minWidth: '150px'
    },
    {
      label: '满意度：一般',
      prop: 'satisfied_2',
      tooltip: true,
      minWidth: '150px'
    },
    {
      label: '满意度：不满意',
      prop: 'satisfied_3',
      tooltip: true,
      minWidth: '150px'
    },
    {
      label: '满意度：非常不满意',
      prop: 'satisfied_4',
      tooltip: true,
      minWidth: '150px'
    },
  ]
  return columns
}

function buildTaskSatisfiedDegreeTableData(props) {
  try {
    let arr = ref([])
    props.pieTableData.forEach((item, index) => {
      let dataSatisfactionPineDataDTOS = item.dataSatisfactionPineDataDTOS || []
      let obj = {
        name: item.name,
        id: item.id,
        // TODO: 微信人员open-data
        userStaffId: item.staffId || '',
        type: item.type,
      }
      dataSatisfactionPineDataDTOS.forEach((degree, degreeIndex) => {
        let keys = `satisfied_${degreeIndex}`
        let taskIdKeys = `${keys}_taskId`
        obj[keys] = `${degree.avgNum}%`
        obj[taskIdKeys] = degree.taskIds
      })
      arr.value.push(obj)
    })
    return arr.value || []
  } catch (e) {
    console.log(e)
    return []
  }
}

function buildTaskSatisfiedDegreePraiseTable(props) {
  try {
    let tableData = ref([])
    let columnsArray = ref([])
    let firstColumns = {
      label: columnsType[props.chartOption?.ousideSelectType],
      prop: 'name',
      tooltip: true,
      minWidth: '150px'
    }
    props.lineTableData.forEach((item, index) => {
      if (index === 0) {
        columnsArray.value = item.dataSatisfactionBrokenDTOList.map((columns, columnsIndex) => {
          return {
            label: columns.statisticsTime,
            prop: `${columns.statisticsTime}_${columnsIndex}`,
            tooltip: true,
            minWidth: '100px'
          }
        })
      }

      let rowMap = {}
      rowMap['name'] = item.name
      // TODO: 微信人员open-data
      rowMap['userStaffId'] = item.staffId || ''
      item.dataSatisfactionBrokenDTOList.forEach((rowItem, rowIndex) => {
        let keys = `${rowItem.statisticsTime}_${rowIndex}`
        let isMostKey = `${keys}_most`
        rowMap[keys] = `${rowItem.avgCount}%`
        rowMap[isMostKey] = rowItem.isMost
        rowMap['id'] = item.id
        rowMap['type'] = item.type
      })
      tableData.value.push(rowMap)
    })
    columnsArray.value?.unshift(firstColumns)
    return {
      columnsData: columnsArray.value,
      tableData: tableData.value
    }
  } catch (e) {
    console.log(e)
  }
}

/** 
 * @description 满意度下钻到工单
*/
export function buildDegreeTaskColumns(data, chartType) {
  if (chartType === 'repeated-maintenance-rate') {
    const { taskColumns, taskTableData } = buildRepeatedMaintenanceRateTaskColumns(data)
    return {
      taskColumns,
      taskTableData
    }
  }
  return {
    taskColumns: buildTaskColumns({ tableData: data }, chartType),
    taskTableData: buildTaskTableData({ tableData: data })
  }
}

/** 
 * @description 处理重复维修率下钻table数据
*/
function buildRepeatedMaintenanceRateTable(props) {
  try {
    let columnsData = []
    let closingData = []
    columnsData = buildCloseColumns(props)
    closingData = buildCloseTableData(props, '0%')
    return {
      columnsData,
      closingData,
    }
  } catch(error) {
    console.log(error)
  }
}

/** 
 * @description 重复维修率下钻到工单
*/
function buildRepeatedMaintenanceRateTaskColumns(data) {
  try {
    let taskColumns = ref([])
    taskColumns.value = [
      {
        label: '工单编号',
        prop: 'taskNo',
        tooltip: true,
        minWidth: 150
      },
      {
        label: '工单类型',
        prop: 'templateName',
        tooltip: true,
        minWidth: 100
      },
      {
        label: '客户',
        prop: 'customerName'
      },
      {
        label: '联系人',
        prop: 'lmUser',
        minWidth: 150
      },
      {
        label: '电话',
        prop: 'lmTel',
        minWidth: 150
      },
      {
        label: '客户地址',
        prop: 'address',
        minWidth: 150,
        tooltip: true,
      },
      {
        label: '产品',
        prop: 'productName',
        minWidth: 150
      },
      {
        label: '服务类型',
        prop: 'serviceType',
        minWidth: 150
      },
      {
        label: '服务内容',
        prop: 'serviceContent',
        minWidth: 150
      },
      {
        label: '服务部门',
        prop: 'serviceTag',
        minWidth: 150
      },
      {
        label: '描述',
        prop: 'description',
        minWidth: 150
      },
      {
        label: '负责人',
        prop: 'userName',
        minWidth: 100
      },
      {
        label: '协同人',
        prop: 'synergiesName',
        minWidth: 100
      }
    ]

    let taskTableData = data || []
    return {
      taskColumns: taskColumns.value, 
      taskTableData
    }
  } catch (error) {
    console.log(error)
  }
}